/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package toesp.bean;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import toesp.entity.Menu;

/**
 *
 * @author satria.sp
 */
public class MenuBean {
    private EntityManagerFactory emf;

    public MenuBean() {
        emf = Persistence.createEntityManagerFactory("swing-researchPU");
    }

    private EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    
    private void getCloseEntityManager() {
        emf.close();
    }
    
    public List<Menu> getMenuHead() throws Exception {       
        List<Menu> lstMenu = null;
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Menu o WHERE o.menuParent=:menuParent ORDER BY o.menuId ASC");
            q.setParameter("menuParent",1);
            lstMenu = q.getResultList();
        } catch (Exception e) {            
        }finally{
            //this.getCloseEntityManager();
        }  
        
        return lstMenu;
    }
    
    public List<Menu> getMenuChild(int p_intParent) throws Exception {       
        List<Menu> lstMenu = null;
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Menu o WHERE o.menuParent=:menuParent ORDER BY o.menuName ASC");   
            q.setParameter("menuParent",p_intParent);
            lstMenu = q.getResultList();
        } catch (Exception e) {            
        }finally{
            //this.getCloseEntityManager();
        }  
        
        return lstMenu;
    } 
    
    public Menu getOnebyId(String p_strMenuName) throws Exception {
        Menu o = null;
         
        try {
            EntityManager em = getEntityManager();
            Query  q = em.createQuery("SELECT o FROM Menu o WHERE o.menuName=:p_strMenuName");   
            q.setParameter("p_strMenuName", p_strMenuName.trim());            
            List<Menu> lstPg = q.getResultList();
            o = (Menu) lstPg.get(0); 
        } catch (Exception e) {            
        }finally{
            //this.getCloseEntityManager();
        }  
        
        return o;
    }
    
    public void commitaj(){
        
    }
}
